// web/src/app/learn/(routes)/search/page.tsx
import React from 'react';
import {db} from '@/lib/db'
import Categories from "@/app/learn/(dashboard)/(routes)/search/_components/categories";
import SearchInput from "@/components/learn/search-input";
import {getCourses} from "@/actions/learn/get-courses";
import {currentUser} from "@/lib/auth";
import {redirect} from "next/navigation";
import CoursesList from "@/components/learn/courses-list";

interface Props {
    searchParams: {
        title: string
        categoryId: string
    }
}

const Page = async ({searchParams}: Props) => {
    const user = await currentUser()
    if (!user) return redirect('/learn')

    const categories = await db.category.findMany({
        orderBy: {name: 'asc'}
    });

    const courses = await getCourses({
        userId: user.id!,
        ...searchParams
    })

    return (
        <>
            <div className={`px-6 pt-6 md:hidden md:mb-0 block`}>
                <SearchInput/>
            </div>
            <div className={`p-6 space-y-4`}>
                <Categories
                    items={categories}
                />
                <CoursesList items={courses}/>
            </div>
        </>
    );
};

export default Page;